package 找到最长的半重复子字符串2730;

public class Solution {
    public int longestSemiRepetitiveSubstring(String s) {
        int left = 0;
        int right = 0;
        int max = 1;
        int count = 0;
        while (right < s.length()) {
            if(right == 0) {
                right++;
                continue;
            }
            if(s.charAt(right) == s.charAt(right - 1)) {
                count++;
            }
            if(count >= 2) {
                if(left == s.length() - 1) {
                    break;
                }
                if(s.charAt(left) == s.charAt(left + 1)) {
                    count--;
                }
                if(s.charAt(right) == s.charAt(right - 1)) {
                    count--;
                }
                left++;
            }else {
                max = Math.max(max, right - left + 1);
                right++;
            }
        }
        return max;
    }
}
